package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.activity.QuanXainOrganizationActivity
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.PartyRegisterAdapter
import cc.vv.btpartyjob.partymanager.delegate.PartyRegisterActivityDelegate
import cc.vv.btpartyjob.partymanager.moudel.bean.PartyReportBean
import cc.vv.btpartyjob.partymanager.moudel.req.PartyReportRequestObj
import cc.vv.btpartyjob.partymanager.moudel.resp.PartyReportResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 党员报到
 * Created by Chen.qi on 2018/10/29.
 */
class PartyRegisterActivity : BaseActivityMVP<PartyRegisterActivityDelegate>()
        , BaseQuickAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {
    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 适配器
     */
    private var mAdapter: PartyRegisterAdapter? = null

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<PartyReportBean>? = null

    /**
     * 社区Id
     */
    private var communityId: String? = null

    /**
     * 网格Id
     */
    private var grid: String? = null

    /**
     * 街道id
     */
    private var streetId: String? = null


    override fun initData() {
        viewDelegate?.setTopBarTitle("党员报到")
        mDataList = ArrayList()
        if (mAdapter == null) {
            mAdapter = PartyRegisterAdapter(R.layout.item_register_party_dy)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }

        val userInfo = UserInfoManageUtil.getUserInfo()
        val userOrgList = userInfo?.userOrgList
        if (null != userOrgList) {
            for (i in userOrgList.indices) {
                when {
                    userOrgList[i].type == 1 -> streetId = userOrgList[i].id
                    userOrgList[i].type == 2 -> communityId = userOrgList[i].id
                    userOrgList[i].type == 3 -> grid = userOrgList[i].id
                }
            }
        }
        val obj = PartyReportRequestObj()
        obj.community = communityId
        obj.grid = grid
        obj.street = streetId
        PartyManagerServer.partyReportList(pageNo, pageSize, obj, true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                val intent = Intent(this@PartyRegisterActivity, QuanXainOrganizationActivity::class.java)
                startActivityForResult(intent, 100)
            }
        })

        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)
        mAdapter?.disableLoadMoreIfNotFullPage()
    }

    override fun getDelegateClass(): Class<PartyRegisterActivityDelegate> {
        return PartyRegisterActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is PartyReportResponseObj -> {
                viewDelegate?.stopRefreshing()
                if (null == obj.data) {
                    return
                }
                if (1 == pageNo) {
                    mDataList?.clear()
                    if (null != obj.data.records) {
                        mDataList!!.addAll(obj.data.records)
                    }
                    viewDelegate?.setAdapterData(mAdapter, mDataList)
                    mAdapter?.disableLoadMoreIfNotFullPage()
                } else {
                    if (mAdapter?.data?.size!! >= obj.data.total) {
                        //数据全部加载完毕
                        mAdapter?.loadMoreEnd(true)
                        mAdapter?.loadMoreEnd()
                    } else {
                        //成功获取更多数据
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        mAdapter?.setNewData(mDataList)
                        //加载数据
                        mAdapter?.disableLoadMoreIfNotFullPage()
                        mAdapter?.loadMoreComplete()
                    }
                }


            }
        }

    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }

    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        val obj = PartyReportRequestObj()
        obj.community = communityId
        obj.grid = grid
        obj.street = streetId
        PartyManagerServer.partyReportList(pageNo, pageSize, obj, true)
    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        val obj = PartyReportRequestObj()
        obj.community = communityId
        obj.grid = grid
        obj.street = streetId
        PartyManagerServer.partyReportList(pageNo, pageSize, obj, true)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val intent = Intent(this, PartyRegisterDetailActivity::class.java)
        intent.putExtra("id", mDataList?.get(position)?.id)
        startActivity(intent)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            pageNo = 1
            communityId = ""
            grid = ""
            streetId = ""
            val orgId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val type = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_TYPE)
            val obj = PartyReportRequestObj()
            when (type) {
                "0" -> {

                }
                "1" -> {
                    streetId = orgId
                }
                "2" -> {
                    communityId = orgId
                }
                "3" -> {
                    grid = orgId
                }
            }
            obj.street = streetId
            obj.community = communityId
            obj.grid = grid
            PartyManagerServer.partyReportList(pageNo, pageSize, obj, true)
        }
    }
}